package cn.tedu.v3_1;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 *  多人聊天室 Version 3
 *
  */

public class Server {
    private static final Integer PORT=1234;
    private ServerSocket serverSocket;
    public Server() {
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (BindException e) {
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start()  {

        try {
            while (true) {
                System.out.println("等待客户端的连接");
               Socket socket=serverSocket.accept();
                System.out.println("一个客户端连接上了连接上了");
                new Thread(new ClientHandler(socket)).start();
            }
        } catch (IOException e) {
            System.out.println("客户端链接失败了");
        } finally {
            if (serverSocket!=null&&serverSocket.isClosed()){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败");
                }
                System.out.println("服务器关闭成功");
            }
        }

    }
    public static void main(String[] args) {
        new Server().start();
    }
    private class ClientHandler implements Runnable {
        private Socket socket;
        private String ip;

        private PrintWriter pw;
        private BufferedReader br;

        public ClientHandler(Socket s){
            //1.明确接入客户端   通讯类 socket
            this.socket=s;
            //2.给明确接入客户端  ip地址
            ip=socket.getInetAddress().getHostAddress();
            //3.给明确接入客户端  读写功能
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
                pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
            } catch (IOException e) {
                System.out.println("客户端断开连接了");
            }
        }
        public void run() {
            try {
             //   BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
              //  pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
            new Thread(()->{
                try {
                    String message;
                    while ((message=br.readLine())!=null){
                        System.out.println(ip+"服务器端说："+message);
                        //1.6增加一句---每次客户端发给服务器端内容
                        System.out.println("服务器端请输入：");
                    }
                }
                catch (IOException e) {
                    System.out.println();
                }
            }).start();
                System.out.println("输入文字内容：");
                Scanner input=new Scanner(System.in);
                while (true) {
                    String say=input.nextLine();
                    pw.println("服务器端说："+say);
                    System.out.println("服务器端输入：");
                }
            } finally {
                if (pw!=null)
                    pw.close();
                if (br != null)
                    try {
                        br.close();
                    }catch (IOException e){
                        System.out.println();
                    }
                //2.关闭CS多线程类中---运行的socket通讯类


                if (socket!=null&&!socket.isClosed()){
                    System.out.println(ip+"一个客户端退出服务器连接");
                    try {
                        socket.close();
                    } catch (IOException e) {
                        System.out.println(ip+"一个客户端退出链接异常");
                    }
                }

                }
            }
        }
    }

